home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / progwin.zip / BTNLOOK.ZIP / BTNLOOK.C < prev    next >
C/C++ Source or Header  |  1991-10-02  |  4KB  |  146 lines

  1.   /*-------------------------------------
  2.     BTNLOOK.C -- Button Look Program
  3.          (c) Charles Petzold, 1990
  4.   ---------------------------------------*/
  5.   #include <windows.h>
  6.   #include <stdio.h>
  7.   struct
  8.        {
  9.        long style;
  10.        char *text;
  11.        }
  12.        button[] =
  13.        {
  14.        BS_PUSHBUTTON,       "PUSHBUTTON",
  15.        BS_DEFPUSHBUTTON,   "DEFPUSHBUTTON",
  16.        BS_CHECKBOX,       "CHECKBOX",
  17.        BS_AUTOCHECKBOX,       "AUTOCHECKBOX",
  18.        BS_RADIOBUTTON,       "RADIOBUTTON",
  19.        BS_3STATE,       "3STATE",
  20.        BS_AUTO3STATE,       "AUTO3STATE",
  21.        BS_GROUPBOX,       "GROUPBOX",
  22.        BS_USERBUTTON,       "USERBUTTON",
  23.        BS_AUTORADIOBUTTON, "AUTORADIO",
  24.        BS_PUSHBOX,       "PUSHBOX"
  25.        };
  26.  
  27.   #define  NUM (sizeof button / sizeof button [0])
  28.  
  29.   long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  30.  
  31.   int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  32.               LPSTR lpszCmdLine, int nCmdShow)
  33.        {
  34.        static char szAppName[] = "BtnLook";
  35.        HWND        hwnd;
  36.        MSG       msg;
  37.        WNDCLASS       wndclass;
  38.  
  39.        if (!hPrevInstance)
  40.         {
  41.         wndclass.style     = CS_HREDRAW | CS_VREDRAW;
  42.         wndclass.lpfnWndProc = WndProc;
  43.         wndclass.cbClsExtra     = 0;
  44.         wndclass.cbWndExtra     = 0;
  45.         wndclass.hInstance     = hInstance;
  46.         wndclass.hIcon     = LoadIcon (NULL, IDI_APPLICATION);
  47.         wndclass.hCursor     = LoadCursor (NULL, IDC_ARROW);
  48.         wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  49.         wndclass.lpszMenuName  = NULL;
  50.         wndclass.lpszClassName = szAppName;
  51.  
  52.         RegisterClass (&wndclass);
  53.         }
  54.        hwnd = CreateWindow (szAppName, "Button Look",
  55.                 WS_OVERLAPPEDWINDOW,
  56.                 CW_USEDEFAULT, CW_USEDEFAULT,
  57.                 CW_USEDEFAULT, CW_USEDEFAULT,
  58.                 NULL, NULL, hInstance, NULL);
  59.  
  60.        ShowWindow (hwnd, nCmdShow);
  61.        UpdateWindow (hwnd);
  62.  
  63.        while (GetMessage (&msg, NULL, 0, 0))
  64.         {
  65.         TranslateMessage (&msg);
  66.         DispatchMessage (&msg);
  67.         }
  68.        return msg.wParam;
  69.        }
  70.  
  71.   long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  72.        {
  73.        static char szPrm[]    = "wParam    LOWORD(lParam)    HIWORD(lParam)",
  74.            szTop[]    = "Control ID    Window Handle    Notification",
  75.            szUnd[]    = "__________    _____________    ____________",
  76.            szFormat[]    = "%5u          %4X        %5u",
  77.            szBuffer[50];
  78.        static HWND hwndButton [NUM];
  79.        static RECT rect;
  80.        static int  cxChar, cyChar;
  81.        HDC       hdc;
  82.        PAINTSTRUCT ps;
  83.        int       i;
  84.        TEXTMETRIC  tm;
  85.  
  86.        switch (message)
  87.         {
  88.         case WM_CREATE :
  89.          hdc = GetDC (hwnd);
  90.          SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
  91.          GetTextMetrics (hdc, &tm);
  92.          cxChar = tm.tmAveCharWidth;
  93.          cyChar = tm.tmHeight + tm.tmExternalLeading;
  94.          ReleaseDC (hwnd, hdc);
  95.  
  96.          for(i = 0; i < NUM; i++)
  97.              hwndButton [i] = CreateWindow ("button", button[i].text,
  98.                 WS_CHILD | WS_VISIBLE | button[i].style,
  99.                 cxChar, cyChar * (1 + 2 * i),
  100.                 20 * cxChar, 7 * cyChar / 4,
  101.                 hwnd, i,
  102.                 ((LPCREATESTRUCT) lParam) -> hInstance, NULL);
  103.              return 0;
  104.         case WM_SIZE :
  105.          rect.left    = 24 * cxChar;
  106.          rect.top    = 3 * cyChar;
  107.          rect.right    = LOWORD (lParam);
  108.          rect.bottom    = HIWORD (lParam);
  109.          return 0;
  110.  
  111.         case WM_PAINT:
  112.         InvalidateRect (hwnd, &rect, TRUE);
  113.  
  114.         hdc = BeginPaint(hwnd,&ps);
  115.         SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
  116.         SetBkMode (hdc, TRANSPARENT);
  117.         TextOut (hdc, 24 * cxChar, 1 * cyChar, szPrm, sizeof szPrm - 1);
  118.         TextOut (hdc, 24 * cxChar, 2 * cyChar, szTop, sizeof szTop - 1);
  119.         TextOut (hdc, 24 * cxChar, 2 * cyChar, szUnd, sizeof szUnd - 1);
  120.  
  121.         EndPaint(hwnd,&ps);
  122.         return 0;
  123.  
  124.         case WM_COMMAND :
  125.          ScrollWindow (hwnd, 0, -cyChar, &rect, &rect);
  126.          hdc = GetDC (hwnd);
  127.          SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
  128.  
  129.          TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
  130.               szBuffer, sprintf (szBuffer, szFormat, wParam,
  131.               LOWORD (lParam), HIWORD (lParam)));
  132.  
  133.          ReleaseDC (hwnd, hdc);
  134.          ValidateRect (hwnd, NULL);
  135.          return 0;
  136.  
  137.         case WM_DESTROY:
  138.         PostQuitMessage(0);
  139.         return 0;
  140.          }
  141.        return DefWindowProc(hwnd,message,wParam,lParam);
  142.        }
  143.  
  144.  
  145.  
  146.